<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$youmid=intval($_RQ['youmid']);
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	if($_SESSION['uid']){
		$this->model('model')->upmember();
		if($youmid==$_SESSION['uid']){
			$youmid=0;
		}
	}
	$_SESSION['youmid']=$youmid;
	if(substr($return['pluginset']['shoplink'],-1)=='/' && substr($return['pluginset']['shoplink'],-2)!='//'){
		$return['pluginset']['shoplink'] .='/';
	}
	$return['pluginset']['shoplink']=$return['pluginset']['shoplink']?:'jy_weishop/index//';
	$return['pluginset']['shoplink'].='&plugin_name=jy_nongren';
	if($youmid){
		$this->model('model')->up_looklog($youmid);
		$return['youmid']=$youmid;
		$member=model('core/member')->detail($youmid,array('credit1','credit2','mobile','avatar','nickname'));
		$newmember=$this->pdo_get('member',array('mid'=>$youmid,'uniacid'=>$_SESSION['uniacid']));
		$isadd=$this->pdo_get('haoyou',array('youmid'=>$youmid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isadd){
			$newmember['ishaoyou']=1;
		}
		$return['member']=array_merge($member,$newmember);
		$return['member']['myji']=$this->pdo_count('myji',array('mid'=>$youmid?:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($return['member']['myji']>0){
			$return['jitips']='好友目前已认养'.$return['member']['myji'].'只蛋鸡，健康蛋鸡'.$return['member']['myji1'].'只！';
		}else{
			$return['jitips']='好友目前还未认养蛋鸡，去通知好友前往兑换或购买《蛋鸡》吧！';
		}
	}else{
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
		$newmember=$this->pdo_get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$return['member']=array_merge($member,$newmember);
		$return['member']['myji']=$this->pdo_count('myji',array('mid'=>$youmid?:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($this->setting['ls_dayfee']>0){
			$isgetls_dayfee=$this->pdo_get('lslog',array('type'=>1,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$isgetls_dayfee){
				$renwu['ls_dayfee']=$this->setting['ls_dayfee'];
			}else{
				$renwu['ls_renwu1']=1;
			}
		}
		if($this->setting['ls_signfee']>0){
			$isgetls_dayfee=$this->pdo_get('lslog',array('type'=>2,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$isgetls_dayfee){
				$renwu['ls_signfee']=$this->setting['ls_signfee'];
			}else{
				$renwu['ls_renwu2']=1;
			}
		}
		if($this->setting['ls_youfee']>0){
			$isgetls_dayfee=$this->pdo_get('lslog',array('type'=>3,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$isgetls_dayfee){
				$renwu['ls_youfee']=$this->setting['ls_youfee'];
			}else{
				$renwu['ls_renwu3']=1;
			}
		}
		if($this->setting['ls_shopfee']>0){
			$isgetls_dayfee=$this->pdo_get('lslog',array('type'=>4,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$isgetls_dayfee){
				$renwu['ls_shopfee']=$this->setting['ls_shopfee'];
			}else{
				$renwu['ls_renwu4']=1;
			}
		}
		if($this->setting['ls_logfee']>0){
			$isgetls_dayfee=$this->pdo_get('lslog',array('type'=>5,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$isgetls_dayfee){
				$renwu['ls_logfee']=$this->setting['ls_logfee'];
			}else{
				$renwu['ls_renwu5']=1;
			}
		}
		$isgetcv_dayfee=$this->pdo_get('cvlog',array('type'=>2,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isgetcv_dayfee){
			$renwu['cv_renwu2']=1;
		}
		$isgetcv_dayfee=$this->pdo_get('cvlog',array('type'=>5,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isgetcv_dayfee){
			$renwu['cv_renwu5']=1;
		}
		$isgetcv_dayfee=$this->pdo_get('cvlog',array('type'=>6,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isgetcv_dayfee){
			$renwu['cv_renwu6']=1;
		}
		$renwu['looklognum']=$this->pdo_count('looklog',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$return['renwu']=$renwu;
		if($return['member']['myji']>0){
			$return['jitips']='您目前已认养'.$return['member']['myji'].'只蛋鸡，健康蛋鸡'.$return['member']['myji1'].'只！';
		}else{
			$return['jitips']='目前还未认养蛋鸡，可以前往兑换或购买《蛋鸡》！';
		}
	}
	$return['member']['haoyounum']=$this->pdo_count('haoyou',array('mid'=>$youmid?:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['member']['noweiji']=$this->pdo_count('myji',array('isweitou'=>0,'status'=>1,'mid'=>$youmid?:$_SESSION['uid'],'weitime <'=>$daytime,'uniacid'=>$_SESSION['uniacid']));
	$return['member']['isweiji']=$this->pdo_count('myji',array('status'=>1,'mid'=>$youmid?:$_SESSION['uid'],'weitime >'=>$daytime,'uniacid'=>$_SESSION['uniacid']));
	$return['member']['myji1']=$this->pdo_count('myji',array('status'=>1,'mid'=>$youmid?:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['params']=array('jy'=>1);
	$mid=$youmid?:$_SESSION['uid'];
	$sharelink='jy_nongren/index//youmid='.$mid.'&fromuser='.$_SESSION['uid'];
	$title=$member['nickname']?$member['nickname'].'的养鸡农场':$return['pluginset']['share_title'];
	$share_image=$member['avatar']?:$return['pluginset']['share_image'];
	$return['sharedata']=array(
			'title'=>$title?:'首页-养鸡农场',
			'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'新概念养鸡农场，一个属于自己的农场！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($share_image)
	);
	set_html('title',$title?:'养鸡农场');
	exi($return);
}
if($_RQ['op']=='upposter'){
	$openid=$_RQ['openid']?:$_SESSION['openid'];
	$posterset=$this->plugin_setting('poster');
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
	$url=custom_url('jy_nongren/index/fromuser='.$_SESSION['uid'],true);
	$qr=cfc('tools')->createqrcode($url,'250',true);
	$qr=tomedia($qr);
	$image='oss/'.$_SESSION['uniacid'].'/images/jy_nongren/wxappid'.$_SESSION['uid'].'.jpg';
	$isadd=cfc('files')->source_check($image);
	if(!$isadd && $_SESSION['wxapp_id']){
			$wxappurl='/jy_nongren/index/index?fromuser='.$_SESSION['uid'];
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			$res=model('core/wxapp')->appcode($wxappurl,$wxapp['appid'],$wxapp['appsecret'],200);
			$path='oss/'.$_SESSION['uniacid'].'/images/jy_nongren/';
			if(!file_exists(ROOT_AT.$path)){
					mkdir(ROOT_AT.$path,0700, true);
			}
			$filename=$path.'/wxappid'.$_SESSION['uid'].'.jpg';
			$qr=cfc('files')->source_save($res,'image','png','二维码',$filename,'',$wxappurl);
	}
	$return=plugin('jy_poster')->get_active('','','',$openid,$qr,$posterset['data'],'jy_nongren');
	$return['posterimg']=$return['poster'];
	exi($return);
}
if($_RQ['op']=='getls_renwu'){
	$type=$_RQ['type'];
	$msg=$this->model('model')->getls_renwu($type);
	exi($msg);
}
if($_RQ['op']=='getcv_renwu'){
	$type=$_RQ['type'];
	$msg=$this->model('cv')->up_renwu(array(),$type,true);
	exi($msg);
}
if($_RQ['op']=='jiahaoyou'){
	$youmid=$_RQ['youmid'];
	$msg=$this->model('model')->jiahaoyou($youmid);
	exi($msg);
}
if($_RQ['op']=='toweiji'){
	$jiid=$_RQ['jiid'];
	$msg=$this->model('model')->toweiji($jiid);
	exi($msg);
}
if($_RQ['op']=='buyls'){
	$postdata=get_postdata();
	$ls_price=$this->setting['ls_price']?:5;
	$postdata['price']=$ls_price;
	$id=$this->model('model')->upfeelog($postdata,1);
	if($ls_price>0){
			$params=array(
					'id'=>$id,
					'title'=>'购买粮食',
					'fee'=>$ls_price,
					'plugin'=>'jy_nongren',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_nongren/index/payresult/',
					'success_url'=>'jy_nongren/index/',
					'error_url'=>'jy_nongren/index/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_nongren_feelog',array('uniacid'=>$_SESSION['uniacid'],'payid'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('购买失败，粮食价格未设置！');
	}
}
if($_RQ['op']=='buyji'){
	$postdata=get_postdata();
	$dj_price=$this->setting['dj_price'];
	$postdata['price']=$dj_price;
	$id=$this->model('model')->upfeelog($postdata,0);
	if($dj_price>0){
			$params=array(
					'id'=>$id,
					'title'=>'认养蛋鸡',
					'fee'=>$dj_price,
					'plugin'=>'jy_nongren',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_nongren/index/payresult/',
					'success_url'=>'jy_nongren/index/',
					'error_url'=>'jy_nongren/index/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_nongren_feelog',array('uniacid'=>$_SESSION['uniacid'],'payid'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('购买失败，蛋鸡价格未设置！');
	}
}
if($_RQ['op']=='weituo'){
	$postdata=get_postdata();
	$item=pdo_get('jy_nongren_myji',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['jiid']));
	$dj_dayfee=$this->setting['dj_dayfee']?:0.8;
	$dj_allday=$this->setting['dj_allday']?:120;
	$item['lastday']=$dj_allday-$item['lsday'];
	$item['lastday']=$item['lastday']>0?$item['lastday']:0;
	$allfee=$dj_dayfee*$item['lastday'];
	$postdata['price']=$allfee;
	$postdata['total']=$item['lastday'];
	$id=$this->model('model')->upfeelog($postdata,2);
	if($allfee>0){
			$params=array(
					'id'=>$id,
					'title'=>'委托平台投喂',
					'fee'=>$allfee,
					'plugin'=>'jy_nongren',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_nongren/index/payresult/',
					'success_url'=>'jy_nongren/index/',
					'error_url'=>'jy_nongren/index/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_nongren_feelog',array('uniacid'=>$_SESSION['uniacid'],'payid'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('购买失败，蛋鸡价格未设置！');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_nongren_feelog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_nongren_feelog',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	if($order['type']==1){
		$lsfee=$order['total']*1000;
		$this->model('model')->getls_renwu(0,$lsfee);
	}elseif($order['type']==2){
		pdo_update('jy_nongren_myji',array('isweitou'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['jiid']));
	}elseif(!$order['type']){
		$this->model('model')->addmyji($order);
	}
	exi('支付成功','success','jy_nongren/index/');
}
